﻿using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace War3Trainer
{
    // Token: 0x02000005 RID: 5
    internal class KeyboardHook
    {
        // Token: 0x14000001 RID: 1
        // (add) Token: 0x06000026 RID: 38 RVA: 0x00002928 File Offset: 0x00000B28
        // (remove) Token: 0x06000027 RID: 39 RVA: 0x00002964 File Offset: 0x00000B64
        public event KeyEventHandler OnKeyDownEvent;

        // Token: 0x14000002 RID: 2
        // (add) Token: 0x06000028 RID: 40 RVA: 0x000029A0 File Offset: 0x00000BA0
        // (remove) Token: 0x06000029 RID: 41 RVA: 0x000029DC File Offset: 0x00000BDC
        public event KeyEventHandler OnKeyUpEvent;

        // Token: 0x14000003 RID: 3
        // (add) Token: 0x0600002A RID: 42 RVA: 0x00002A18 File Offset: 0x00000C18
        // (remove) Token: 0x0600002B RID: 43 RVA: 0x00002A54 File Offset: 0x00000C54
        public event KeyPressEventHandler OnKeyPressEvent;

        // Token: 0x0600002C RID: 44
        [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
        public static extern int SetWindowsHookEx(int idHook, KeyboardHook.HookProc lpfn, IntPtr hInstance, int threadId);

        // Token: 0x0600002D RID: 45
        [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
        public static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);

        // Token: 0x0600002E RID: 46
        [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
        public static extern bool UnhookWindowsHookEx(int idHook);

        // Token: 0x0600002F RID: 47
        [DllImport("kernel32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
        private static extern IntPtr GetModuleHandle(string lpModuleName);

        // Token: 0x06000030 RID: 48 RVA: 0x00002A90 File Offset: 0x00000C90
        private int KeyboardHookProc(int nCode, int wParam, IntPtr lParam)
        {
            if (nCode >= 0 && (this.OnKeyDownEvent != null || this.OnKeyUpEvent != null || this.OnKeyPressEvent != null))
            {
                KeyboardHook.KeyboardHookStruct MyKBHookStruct = (KeyboardHook.KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHook.KeyboardHookStruct));
                if (this.OnKeyDownEvent != null && (wParam == 256 || wParam == 260))
                {
                    Keys keyData = (Keys)MyKBHookStruct.vkCode;
                    KeyEventArgs e = new KeyEventArgs(keyData);
                    this.OnKeyDownEvent(this, e);
                }
            }
            return KeyboardHook.CallNextHookEx(KeyboardHook.hKeyboardHook, nCode, wParam, lParam);
        }

        // Token: 0x06000031 RID: 49 RVA: 0x00002B3C File Offset: 0x00000D3C
        public void Start()
        {
            if (KeyboardHook.hKeyboardHook == 0)
            {
                this.KeyboardHookProcedure = new KeyboardHook.HookProc(this.KeyboardHookProc);
                using (Process curProcess = Process.GetCurrentProcess())
                {
                    using (ProcessModule curModule = curProcess.MainModule)
                    {
                        KeyboardHook.hKeyboardHook = KeyboardHook.SetWindowsHookEx(13, this.KeyboardHookProcedure, KeyboardHook.GetModuleHandle(curModule.ModuleName), 0);
                    }
                }
                if (KeyboardHook.hKeyboardHook == 0)
                {
                    this.Stop();
                    throw new Exception("Set GlobalKeyboardHook failed!");
                }
            }
        }

        // Token: 0x06000032 RID: 50 RVA: 0x00002C00 File Offset: 0x00000E00
        public void Stop()
        {
            bool retKeyboard = true;
            if (KeyboardHook.hKeyboardHook != 0)
            {
                retKeyboard = KeyboardHook.UnhookWindowsHookEx(KeyboardHook.hKeyboardHook);
                KeyboardHook.hKeyboardHook = 0;
            }
            if (!retKeyboard)
            {
            }
        }

        // Token: 0x06000033 RID: 51 RVA: 0x00002C36 File Offset: 0x00000E36
        public KeyboardHook()
        {
            this.Start();
        }

        // Token: 0x06000034 RID: 52 RVA: 0x00002C48 File Offset: 0x00000E48
        ~KeyboardHook()
        {
            this.Stop();
        }

        // Token: 0x0400000E RID: 14
        private const int WM_KEYDOWN = 256;

        // Token: 0x0400000F RID: 15
        private const int WM_KEYUP = 257;

        // Token: 0x04000010 RID: 16
        private const int WM_SYSKEYDOWN = 260;

        // Token: 0x04000011 RID: 17
        private const int WM_SYSKEYUP = 261;

        // Token: 0x04000012 RID: 18
        public const int WH_KEYBOARD_LL = 13;

        // Token: 0x04000016 RID: 22
        private static int hKeyboardHook = 0;

        // Token: 0x04000017 RID: 23
        private KeyboardHook.HookProc KeyboardHookProcedure;

        // Token: 0x02000006 RID: 6
        // (Invoke) Token: 0x06000037 RID: 55
        public delegate int HookProc(int nCode, int wParam, IntPtr lParam);

        // Token: 0x02000007 RID: 7
        [StructLayout(LayoutKind.Sequential)]
        public class KeyboardHookStruct
        {
            // Token: 0x04000018 RID: 24
            public int vkCode;

            // Token: 0x04000019 RID: 25
            public int scanCode;

            // Token: 0x0400001A RID: 26
            public int flags;

            // Token: 0x0400001B RID: 27
            public int time;

            // Token: 0x0400001C RID: 28
            public int dwExtraInfo;
        }
    }
}
